<template>
  <div id="detail">
    <detail-nav-bar @itemClick="itemClick" ref="navBar"/>
    <scroll class="wrapper" ref="scroll" :probeType="3" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"/>
      <detail-param-info :param-info="paramInfo" ref="params"/>
      <detail-comment-info :comment-info="commentInfo" ref="comment"/>
      <goods-list :goods="recommendInfo" ref="recommend"/>
    </scroll>
    <back-top v-if="showBackTop" @click.native="backTop"/>
    <detail-tab-bar @addToCart="addToCart"/>
  </div>
</template>

<script>
  import DetailNavBar from "./childComps/DetailNavBar";
  import DetailSwiper from "./childComps/DetailSwiper";
  import DetailBaseInfo from "./childComps/DetailBaseInfo";
  import DetailShopInfo from "./childComps/DetailShopInfo";
  import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
  import DetailParamInfo from "./childComps/DetailParamInfo";
  import DetailCommentInfo from "./childComps/DetailCommentInfo";
  import DetailTabBar from "./childComps/DetailTabBar";


  import Scroll from "components/common/scroll/Scroll";
  import GoodsList from "components/content/goods/GoodsList";

  import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "network/detail";
  import {itemListenerMixin, backTopMixin} from "common/mixin";

  export default {
    name: "Detail",
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      DetailTabBar,

      Scroll,
      GoodsList
    },
    mixins: [itemListenerMixin, backTopMixin],
    created() {
      this.iid = this.$route.params.iid
      getDetail(this.iid).then(res => {
        const data = res.result
        //1.获取顶部轮播图
        this.topImages = data.itemInfo.topImages

        // 2.获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

        // 3.创建店铺信息的对象
        this.shop = new Shop(data.shopInfo)

        // 4.详细信息
        this.detailInfo = data.detailInfo

        // 5.获取参数的信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

        // 6.评论信息
        if (data.rate.list) {
          this.commentInfo = data.rate.list[0];
        }
      })

      getRecommend().then(data => {
        this.recommendInfo = data.data.list
      })


    },
    mounted() {

    },
    destroyed() {
      this.$bus.$off('itemImgLoad', this.itemImgListener)
    },
    data() {
      return {
        iid: 0,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommendInfo: [],
        itemImgListener: null,
        themeTopYs: [],
        currentIndex: 0
      }
    },
    methods: {
      imageLoad() {
        this.$refs.scroll.refresh()
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
        this.themeTopYs.push(Number.MAX_VALUE)
      },

      itemClick(value) {
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[value])
      },

      contentScroll(position) {
        const positionY = -position.y
        this.showBackTop = positionY > 1000
        const length = this.themeTopYs.length
        for (let i = 0; i < length - 1; i++) {
          if (this.currentIndex !== i && (i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
            this.currentIndex = i
            this.$refs.navBar.currentIndex = this.currentIndex
          }
        }
      },

      addToCart() {
        // 1.获取商品信息
        const product = {}
        product.iid = this.iid
        product.imgURL = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.realPrice
        product.count = 1

        // 2.添加到vuex状态
        this.$store.dispatch('addToCart', product).then(res => {
         this.$toast({message: '加入购物车成功'})
        })
      }
    }
  }
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 10;
    background-color: #ffffff;
    height: 100vh;
  }

  .wrapper {
    height: calc(100% - 93px);
  }
</style>